<?php

class HomeController extends BaseController {
	public $crud = false;

	public function dashboard() {
		$data['total_per_status'] = $this->_getItemTotals();

		return View::make('dashboard-index')->with($data);
	}
	
	/**
	 * Get Per Order Item Status Total.
	 * @return mixed
	 */
	private function _getItemTotals() {
		// @todo: Add Forfeited
		$order_basis = array(
			Constant::ORDER_TOPUP,
			Constant::ORDER_PROCESSING,
			Constant::ORDER_FOR_DELIVERY,
			Constant::ORDER_DELIVERED,
			Constant::ORDER_BILLED,
			Constant::ORDER_CANCELLED
		);
		
		// Total Count Per Order Status
		$order_per_status = Order::active()
			->has('orderProducts')
			->groupBy('order.status')
			->distinct('order.id')
			->whereIn('order.status', $order_basis)
			->get( array('order.status', DB::raw('COUNT(order.id) AS total')) )->toArray();
		
		$item_basis = array(
				Constant::ORDER_ORDERED_FROM_SUPPLIER,
				Constant::ORDER_RECEIVED,
		);
		
		$totals = $this->_getItemTotalsAttributes();
		
		// Retrieve Pending P. O. ( PO LEVEL)
		$pendingPo = PurchaseOrder::active()
			->where('status', Constant::PO_PENDING)
			->count();
		$totals[ Constant::ORDER_ORDERED_FROM_SUPPLIER ]['total'] = $pendingPo;
		
		// Retrieve Approved P. O. unreceived items ( ITEM LEVEL)
		$pendingReceived = PurchaseOrderProduct::active()
			->whereHas('PurchaseOrder', function ( $po ) {
				$po->where('status', Constant::PO_APPROVED);
			})
			->where('receive_include', 0)
			->count();
		$totals[ Constant::ORDER_RECEIVED ]['total'] = $pendingReceived;

		foreach ( $order_per_status as $total )
			$totals[ $total['status'] ]['total'] = $total['total'];

		return $totals;
	}
	
	/** 
	 * Retrieves default styling for Order Item Status attributes.
	 * @return array
	 */
	private function _getItemTotalsAttributes() {
		$print_status = Constant::getOrderList( true );
		$orig_status = Constant::getOrderList();

		return array(
			Constant::ORDER_TOPUP => array(
				'id' 				=> $orig_status[Constant::ORDER_TOPUP],
				'title' 				=> $print_status[Constant::ORDER_TOPUP] . 's ',
				'icon' 			=> '',
				'total' 		 	=> 0,
				'background' 	=> '#6600CC',
				'rate'				=> 0,
				'percentage'	=> 0,
				'total'				=> 0,
				'view_more'	=> URL::to('top-up/preview-list'),
				'main_url'		=> URL::to('top-up')
			),
			Constant::ORDER_PROCESSING => array(
				'id' 				=> $orig_status[Constant::ORDER_PROCESSING],
				'title' 				=> $print_status[Constant::ORDER_PROCESSING] . ' Orders',
				'icon' 			=> '',
				'total' 		 	=> 0,
				'background' 	=> '#F9BF3B',
				'rate'				=> 0,
				'percentage'	=> 0,
				'total'				=> 0,
				'view_more'	=> URL::to('processing/preview-list'),
				'main_url'		=> URL::to('processing')
			),
			Constant::ORDER_ORDERED_FROM_SUPPLIER => array(
				'id' 				=> $orig_status[Constant::ORDER_ORDERED_FROM_SUPPLIER],
				'title' 				=> 'Pending Purchase Orders',
				'icon' 			=> '',
				'total' 		 	=> 0,
				'background' 	=> '#003366',
				'rate'				=> 0,
				'percentage'	=> 0,
				'total'				=> 0,
				'view_more'	=> URL::to('po/preview-list/po/0,2'),
				'main_url'		=> URL::to('po') . '?filter_status=' . Constant::PO_PENDING
			),
			Constant::ORDER_RECEIVED => array(
				'id' 				=> $orig_status[Constant::ORDER_RECEIVED],
				'title' 				=> 'Items To Receive',
				'icon' 			=> '',
				'total' 		 	=> 0,
				'background' 	=> '#B6212D',
				'rate'				=> 0,
				'percentage'	=> 0,
				'total'				=> 0,
				'view_more'	=> URL::to('po/preview-list/item/1'),
				'main_url'		=> URL::to('po') . '?filter_status=' . Constant::PO_APPROVED
			),
			Constant::ORDER_FOR_DELIVERY => array(
				'id' 				=> $orig_status[Constant::ORDER_FOR_DELIVERY],
				'title' 				=> 'Ready ' . $print_status[Constant::ORDER_FOR_DELIVERY] . ' Orders',
				'icon' 			=> '',
				'total' 		 	=> 0,
				'background' 	=> '#009966',
				'rate'				=> 0,
				'percentage'	=> 0,
				'total'				=> 0,
				'view_more'	=> URL::to('for-delivery/preview-list'),
				'main_url'		=> URL::to('for-delivery')
			),
			Constant::ORDER_DELIVERED => array(
				'id' 				=> $orig_status[Constant::ORDER_DELIVERED],
				'title' 				=> $print_status[Constant::ORDER_DELIVERED] . ' Orders',
				'icon' 			=> '',
				'total' 		 	=> 0,
				'background' 	=> '#8A949B',
				'rate'				=> 0,
				'percentage'	=> 0,
				'total'				=> 0,
				'view_more'	=> URL::to('order/preview-list/' . Constant::ORDER_DELIVERED),
				'main_url'		=> URL::to('order') . '?filter_status=' . Constant::ORDER_DELIVERED
			),
			Constant::ORDER_CANCELLED => array(
				'id' 				=> $orig_status[Constant::ORDER_CANCELLED],
				'title' 				=> $print_status[Constant::ORDER_CANCELLED] . ' Orders',
				'icon' 			=> '',
				'total' 		 	=> 0,
				'background' 	=> '#9D538E',
				'rate'				=> 0,
				'percentage'	=> 0,
				'total'				=> 0,
				'view_more'	=> URL::to('order/preview-list/' . Constant::ORDER_CANCELLED),
				'main_url'		=> URL::to('order') . '?filter_status=' . Constant::ORDER_CANCELLED
			),
			Constant::ORDER_BILLED => array(
				'id' 				=> $orig_status[Constant::ORDER_BILLED],
				'title' 				=> $print_status[Constant::ORDER_BILLED] . ' Orders',
				'icon' 			=> '',
				'total' 		 	=> 0,
				'background' 	=> '#400D12',
				'rate'				=> 0,
				'percentage'	=> 0,
				'total'				=> 0,
				'view_more'	=> URL::to('order/preview-list/' . Constant::ORDER_BILLED),
				'main_url'		=> URL::to('order') . '?filter_status=' . Constant::ORDER_BILLED
			)
		);
	}
}
